package com.woniuxy.service.bed;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Bed;
import com.woniuxy.entity.Home;
import com.woniuxy.mapper.BedMapper;
import com.woniuxy.mapper.HomeMapper;
import com.woniuxy.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 马踏东京赏樱花
 */
@Service
@Transactional
public class BedServiceImpl implements BedService {
    @Autowired
    private BedMapper bedMapper;
    @Autowired
    private HomeMapper homeMapper;

    @Override
    public int updateById(Integer bedId) {
        int rows = bedMapper.updateById(bedId);
        return rows;
    }

    @Override
    public int deleteOne(String str) {
        if (str == null) {
            throw new RuntimeException("数据获取失败");
        }
        Bed bed = new Bed();
        bed.setBedId(Integer.valueOf(str));
        return bedMapper.deleteByPrimaryKey(bed.getBedId());
    }

    @Override
    public Bed findOne(String str) {
        if (str == null) {
            throw new RuntimeException("数据获取失败");
        }
        Bed bed = new Bed();
        bed.setBedId(Integer.valueOf(str));
        return bedMapper.selectByPrimaryKey(bed.getBedId());
    }

    @Override
    public List<Bed> findAll() {
        return bedMapper.selectByExample(null);
    }

    @Override
    public int updateOne(Bed bed) {
        int rows = bedMapper.updateByPrimaryKey(bed);
        if (rows > 0) {
            return rows;
        }
        throw new RuntimeException("修改失败");
    }
    @Override
    public PageInfo<Bed> findByPage(int page, int size) {
        PageHelper.startPage(page, size);
        List<Bed> list = bedMapper.selectByExample(null);
        Home home = new Home();
        for (Bed bed : list) {
            home = homeMapper.selectByPrimaryKey(bed.getHomeId());
            bed.setHomeName(home.getHomeName());
        }
        PageInfo info = new PageInfo(list);
        return info;
    }

    @Override
    public int save(Bed bed) {
        return bedMapper.insertSelective(bed);
    }

    @Override
    public List<Home> findAllHome(Integer hId,Integer deptId) {
        List<Home> allHome = homeMapper.findAllHome(hId,deptId);
        return allHome;
    }

}

